<template>
	<coreshop-page :title="state.title" mode="left">
		<view class="layout-notice-detail">
			<view class="notice-info">
				<view class="title">{{ state.articleDetail?.title }}</view>
				<view class="time">{{ state.articleDetail?.createTime }}</view>
				<view class="content">
					<uv-parse :content="state.articleDetail?.contentBody"></uv-parse>
				</view>
			</view>
		</view>
	</coreshop-page>
</template>
<script setup lang="ts">
	import { reactive } from 'vue';
	import { onLoad, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app';
	import { queryArticleDetail, queryNoticeInfo, queryMessageDetil } from '@/core/api';
	import type { Response } from '@/core/models';
	import { handleShowToast, handleRouteSwitchTab ,getShareUrl} from '@/core/utils';
	import { ShareClientEnum, ShareModelEnum, ShareEnum, ArticleDetailEnum } from '@/core/enum';
	import { shareUrl } from '@/core/consts'

	interface QueryParams {
		id : number;
		idType : number;
	}

	const state = reactive<{
		articleDetail : any;
		idType : number;
		id : number;
		title : string;
		shareUrl:string;
	}>({
		articleDetail: {},
		idType: 1,
		id: 0,
		title: "文章详情",
		shareUrl:"", 
	})

	onLoad(async (query : QueryParams) => {
		state.idType = query?.idType || 1;
		state.id = query?.id || 0;

		if (state.idType == ArticleDetailEnum.article) {
			/** 获取文章详情 */
			getArticleDetail();
			state.title = '文章详情';
		} else if (state.idType == ArticleDetailEnum.notice) {
			/** 获取公告详情 */
			getNoticeDetail();
			state.title = '公告详情';
		} else if (state.idType == ArticleDetailEnum.message) {
			/** 获取图文消息 */
			getMessageDetail();
			state.title = '图文消息';
		} else {
			handleShowToast('获取失败', 'error', () => {
				handleRouteSwitchTab();
			})
		}
		/** 获取分享url */ 
		state.shareUrl = await getShareUrl({
			client: ShareClientEnum.wxMiNiProgram,
			url: shareUrl,
			type: ShareModelEnum.url,
			page: ShareEnum.article,
			params: {
				articleId: state.id,
				articleType: state.idType
			}
		})
	})

	/** 获取文章详情 */
	const getArticleDetail = async () => {
		uni.showLoading({
			title: "加载中..."
		})
		const articleDetail : Response<any> = await queryArticleDetail({ id: state.id });
		uni.hideLoading();
		if (articleDetail.status) {
			state.articleDetail = articleDetail.data;
		} else {
			handleShowToast(articleDetail.msg)
		}
	}

	/** 获取公告详情 */
	const getNoticeDetail = async () => {
		uni.showLoading({
			title: "加载中..."
		})
		const articleDetail : Response<any> = await queryNoticeInfo({ id: state.id });
		uni.hideLoading();
		if (articleDetail.status) {
			state.articleDetail = articleDetail.data;
		} else {
			handleShowToast(articleDetail.msg)
		}
	}

	/** 获取图文消息 */
	const getMessageDetail = async () => {
		uni.showLoading({
			title: "加载中..."
		})
		const articleDetail : Response<any> = await queryMessageDetil({ id: state.id });
		uni.hideLoading();
		if (articleDetail.status) {
			state.articleDetail = articleDetail.data;
		} else {
			handleShowToast(articleDetail.msg)
		}
	} 

	/** 分享 */
	onShareAppMessage(() => {
		return {
			title: state.articleDetail.title,
			path: state.shareUrl
		}
	});
	onShareTimeline(() => {
		return {
			title: state.articleDetail.title,
			path: state.shareUrl
		}
	});
</script>
<style lang="scss" scoped>
	@import './detail.scss';
</style>